package com.whale.cloud.util.rest.query;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.whale.cloud.util.jdbc.JdbcDataEngine;
import com.whale.cloud.util.rest.condition.EColumnAuthority;
import com.whale.cloud.util.rest.condition.ETableAuthority;
import com.whale.cloud.util.rest.sql.SqlDefine;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by paco on 2017/06/07.
 */
@Log4j2
@Component
public class GetSysConfigure {

		public static final GetSysConfigure getSc = new GetSysConfigure();
		private static final int authorityCode = 1024;
		@Autowired
		private JdbcDataEngine jdbcDataEngine;

		public static GetSysConfigure getInstance() {
				return getSc;
		}

		/**
		 * 根据表ID获取表信息
		 *
		 * @param tableId
		 * @return
		 */
		public JSONObject getTableById(long tableId) {
				//region 1、如果REDIS有数据，则从REDIS读取

				//endregion

				//region 2、如果REDIS没有数据，则从数据库读取
				JSONObject tablejo =
								jdbcDataEngine
												.getJdbcClient()
												.getJSONObject(SqlDefine.GetSysConfigure_getFunctionById_sql, new Object[]{tableId});
				return tablejo;
				//endregion
		}

		/**
		 * 根据表名称获取表信息
		 *
		 * @param tableDBName
		 * @return
		 */
		public JSONObject getTableByDBName(String tableDBName) {
				//region 1、如果REDIS有数据，则从REDIS读取

				//endregion

				//region 2、如果REDIS没有数据，则从数据库读取
				JSONObject tablejo =
								jdbcDataEngine
												.getJdbcClient()
												.getJSONObject(
																SqlDefine.GetSysConfigure_getFunctionByDBName_sql, new Object[]{tableDBName});

				//endregion
				return tablejo;
		}

		/**
		 * 根据手段ID获取字段信息
		 *
		 * @param columnId
		 * @return
		 */
		public JSONObject getColumnById(long columnId) {
				//region 1、如果REDIS有数据，则从REDIS读取

				//endregion

				//region 2、如果REDIS没有数据，则从数据库读取
				JSONObject columnjo =
								jdbcDataEngine
												.getJdbcClient()
												.getJSONObject(SqlDefine.GetSysConfigure_getColumnById_sql, new Object[]{columnId});
				//endregion
				return columnjo;
		}

		/**
		 * 根据字段名称获取字段信息
		 *
		 * @param tableId
		 * @param columnDBName
		 * @return
		 */
		public JSONObject getColumnByDBName(long tableId, String columnDBName) {
				//region 1、如果REDIS有数据，则从REDIS读取

				//endregion

				//region 2、如果REDIS没有数据，则从数据库读取
				JSONObject columnjo =
								jdbcDataEngine
												.getJdbcClient()
												.getJSONObject(
																SqlDefine.GetSysConfigure_getColumnByDBName_sql,
																new Object[]{tableId, columnDBName});
				//endregion
				return columnjo;
		}

		/**
		 * 根据表ID获取字段信息
		 *
		 * @param tableId
		 * @return
		 */
		public JSONArray getColumnByTableId(long tableId) {
				//region 1、如果REDIS有数据，则从REDIS读取

				//endregion

				//region 2、如果REDIS没有数据，则从数据库读取
				JSONArray columnjo =
								jdbcDataEngine
												.getJdbcClient()
												.getJSONArray(SqlDefine.GetSysConfigure_getColumnByDBName_sql, new Object[]{tableId});
				//endregion

				return columnjo;
		}

		/**
		 * 根据表名称获取字段信息
		 *
		 * @param tableDBName
		 * @return
		 */
		public JSONArray getColumnByTableDBName(String tableDBName) {
				//region 1、如果REDIS有数据，则从REDIS读取

				//endregion

				//region 2、如果REDIS没有数据，则从数据库读取
				JSONArray columnjo =
								jdbcDataEngine
												.getJdbcClient()
												.getJSONArray(
																SqlDefine.GetSysConfigure_getColumnByDBName_sql, new Object[]{tableDBName});
				//endregion

				return columnjo;
		}

		/**
		 * 查询具有相应操作权限的字段
		 *
		 * @param tableId 表ID
		 * @param eca     权限
		 * @return
		 */
		public JSONArray getAuthorityColumns(long tableId, long userId, EColumnAuthority eca) {
				JSONArray userAuthorityColumns = new JSONArray();

				//region 1、如果REDIS有数据，则从REDIS读取

				//endregion

				//region 2、如果REDIS没有数据，则从数据库读取
				String authcode =
								String.format(
												"%010d", Integer.valueOf(Integer.toBinaryString(authorityCode >> eca.getValue())));
				authcode = authcode.replace("0", "_");
				userAuthorityColumns =
								jdbcDataEngine
												.getJdbcClient()
												.getJSONArray(
																SqlDefine.GetSysConfigure_getUserColumnById_sql,
																new Object[]{tableId, userId, userId, authcode});

				//endregion

				return userAuthorityColumns;
		}

		/**
		 * 判断用户是否有表的相应操作权限
		 *
		 * @param tableId 要核验的表ID
		 * @param userId  要核验的用户ID
		 * @param eta     要判断的权限
		 * @return
		 */
		public boolean checkAuthorityTable(long tableId, long userId, ETableAuthority eta) {
				boolean isAuthority = false;
				int count = 0;
				//region 1、如果REDIS有数据，则从REDIS读取

				//endregion

				//region 2、如果REDIS没有数据，则从数据库读取
				count =
								jdbcDataEngine
												.getJdbcClient()
												.getCount(
																SqlDefine.GetSysConfigure_getUserFunctionCount_sql,
																new Object[]{tableId, userId, userId, eta.getValue()});

				isAuthority = count > 0;
				//endregion

				return isAuthority;
		}

		public JSONArray getUserSearchColumns(long functionId, long userId) {
				JSONArray searchColumnsja =
								jdbcDataEngine
												.getJdbcClient()
												.getJSONArray(
																SqlDefine.GetSysConfigure_getUserSearchColumns_sql,
																new Object[]{functionId, userId, userId});
				return searchColumnsja;
		}

		public JSONArray getOptions(long optionId) {

				//region 1、如果REDIS有数据，则从REDIS读取

				//endregion

				//region 2、如果REDIS没有数据，则从数据库读取
				JSONArray optionja =
								jdbcDataEngine
												.getJdbcClient()
												.getJSONArray(SqlDefine.GetSysConfigure_getOptionItmesById_sql, new Object[]{optionId});

				return optionja;
				//endregion
		}
}
